angular.module('musicList.controllers', ['ionic']) //歌手歌单指令
.controller('musicListCtrl',function($scope, Chats,$http,$stateParams,$timeout,$ionicModal) {
	$scope.navback = function(){ //回退按钮
		var hre = './#/tab/home';
      		location.href = hre;
	}
	$ionicModal.fromTemplateUrl('templates/modal.html', {
	    scope: $scope
	  }).then(function(modal) {
	    $scope.modal = modal;
	  });
	$scope.createContact = function(u) {        
	    $scope.contacts.push({ name: u.firstName + ' ' + u.lastName });
	    $scope.modal.hide();
	  };
	$scope.loadnum = 0;
	$scope.qingqiu = $stateParams.mylist; // 判断是那个页面做的请求
	$scope.changid = $stateParams.chatId; //列表id
	$scope.songlist = [];
	$scope.loadmoreoff = true;
	$scope.searchoff = true;
	$scope.toplist = function(){ //排行榜请求
		$scope.musicurl ="from=qianqian&version=2.1.0&method=baidu.ting.billboard.billList&format=json&type="
		+ $scope.changid + "&offset=" + $scope.loadnum + "&size=30";
		Chats.music($scope.musicurl).then(function(data){  
			//  console.log(data)	
				
			if(!data || data.data.errno){
				$('#contentlist').html('请求的页面不存在');
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$scope.searchoff = false;
				return;
			}
				$scope.songlist = $scope.songlist.concat(data.data.song_list)
				$scope.data = data.data; //列表信息					
				$scope.firstsong = $scope.songlist[0].song_id;//第一首歌id
				$scope.x_img = $scope.data.billboard.pic_s210; //列表头部照片
				$scope.title = '排行榜' //列表标题;
				$scope.firstsong = $scope.songlist[0].song_id; //全部播放传递的歌曲id
				$scope.name =  $scope.data.billboard.name;
				$scope.loadmoreoff = true;
				$scope.$broadcast('scroll.infiniteScrollComplete');					
		})	
	}
	
	$scope.vidolist = function(){ //电台列表请求
		$scope.musicurl ="from=qianqian&version=2.1.0&method=baidu.ting.radio.getChannelSong&format=json&pn=0&rn=10&channelname="
		+ $scope.changid;
		Chats.music($scope.musicurl).then(function(data){ 
			// console.log(data)
			
			if(!data || data.data.errno){
					$('#contentlist').html('请求的页面不存在');
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.searchoff = false;
					return;
				}
				var song = data.data.result.songlist;
				song.splice(song.length - 1,1);
				$scope.songlist = $scope.songlist.concat(song);
				$scope.data = data.data; //列表信息
				$scope.firstsong = song[0].songid; //全部播放传递的歌曲id
				$scope.x_img = song[0].thumb;//列表头部照片
				$scope.title = '电台';//列表标题
				$scope.name = $scope.data.result.channel;
				$scope.loadmoreoff = true;
				$scope.$broadcast('scroll.infiniteScrollComplete');
			
		})
	}

	$scope.searchlist = function(){ //搜索页面请求
		$scope.musicurl ="method=baidu.ting.search.catalogSug&query="
		+ $scope.changid;
				Chats.music($scope.musicurl).then(function(data){
				// console.log(data)
			
				if(!data || data.data.errno){
					$('#contentlist').html('请求的页面不存在');
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.searchoff = false;
					return;
				}			
				$scope.data = data.data; //列表信息	
				$scope.songlist = $scope.songlist.concat(data.data.song)							
				$scope.firstsong = $scope.songlist[0].song_id;//第一首歌id
				$scope.x_img = $scope.data.artist[0].artistpic; //列表头部照片
				$scope.title = "搜索" //列表标题;
				$scope.firstsong = $scope.songlist[0].songid; //全部播放传递的歌曲id
				$scope.name =  $scope.changid;
				$scope.loadmoreoff = true;
				$scope.searchoff = false;
				$scope.$broadcast('scroll.infiniteScrollComplete');										
			})				
	}

	//导航条滚动监听事件
	$('.x_content').scroll(function(data){  
		var header = $('.x_imgfilter').last().innerHeight();
		var navheight = $('.x_nav').innerHeight();
		var scrollTop = data.originalEvent.target.scrollTop || data.originalEvent.detail.scrollTop
				// console.log(scrollTop);;
		if(scrollTop + navheight >= header){
			$('.x_nav').css({
				'background':'url(' + $scope.x_img + ')',
				'background-repeat': 'no-repeat',
				'background-position':'top left',
				'background-size': '1000%' 
			})
			$('.x_nav_title').text($scope.name)
		}else{
			$('.x_nav').css({'background':'none'})
			$('.x_nav_title').text($scope.title)
		}			
	})
	$scope.currentPlaying = function(i){ //点击出现音乐播放图标	
		for(var j = 0 ;j<=$scope.songlist.length;j++){			
			$('.x_playing').eq(j).html(j+1)
			.css({'color':'black'})
		}
		$('.x_list').eq(i).find('.x_playing').html('<span class="ion-volume-medium"></span>')
		.css({'color':'red'})
	}
	$scope.doRefresh = function(){
		if($scope.qingqiu == "radio"){ //电台请求
			$scope.vidolist();
		}
		$scope.$broadcast('scroll.refreshComplete');
	};
	$scope.loadMore = function(){
//		console.log("刷新了");
		$scope.loadnum++;
		if($scope.loadmoreoff){			
			if($scope.qingqiu == "billboard"){ //排行榜请求
				$scope.toplist();
			}
			if($scope.qingqiu == "radio"){ //电台请求
				$scope.vidolist();
			}
			if($scope.qingqiu == "search"){ //搜索请求				
				$scope.searchlist();							
			}
		}
		$scope.loadmoreoff = false;
	}
})